﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SuperFox.SuperServiceReference;
using System.Windows.Data;

namespace SuperFox
{
    public partial class clientOrderForm : UserControl
    {
        public clientOrderForm()
        {
            InitializeComponent();

            // create a new order for the user to fill in


            if ((App.Current.RootVisual as MainPage).CurrentUser.UserOrderInfo == null)
            {
                (App.Current.RootVisual as MainPage).CurrentUser.UserOrderInfo = new Order();
            }
            
            this.DataContext = (App.Current.RootVisual as MainPage).CurrentUser;

            


            BranchCb.ItemsSource = (App.Current.RootVisual as MainPage).CurrentUser.Programs.Brnaches;
            CostCb.ItemsSource = (App.Current.RootVisual as MainPage).CurrentUser.Programs.CostCenters;
            CapmaignTitle.Text = (App.Current.RootVisual as MainPage).CurrentUser.Programs.Style.OrderFormReference + ":";
            CompanyNameTb.Text = (App.Current.RootVisual as MainPage).CurrentUser.Programs.merc_name;


     
            //Binding bb = new Binding()
            //{
            //    Source = (App.Current.RootVisual as MainPage).CurrentUser.Programs.Reqfields.YourDetailsFirstName,
            //    Path = new PropertyPath("YourDetailsFirstName"),
            //    Mode = BindingMode.OneWay
            //};

            //TitleComboBox.Required = (App.Current.RootVisual as MainPage).CurrentUser.Programs.Reqfields.YourDetailsTitle;

            //FirstNameTb.SetBinding(TextBoxExtension.RequiredProperty, bb);

            
            //SurnameTb.Required = (App.Current.RootVisual as MainPage).CurrentUser.UserOrderInfo.ShippingInfo.PersonInfo.Title;
            //EmailTb.Required = (App.Current.RootVisual as MainPage).CurrentUser.Programs.Reqfields.YouDetailsEmail;
            //CompanyTb.Required = (App.Current.RootVisual as MainPage).CurrentUser.Programs.Reqfields.YourDetailsCompany;
            //TelephoneTb.Required = (App.Current.RootVisual as MainPage).CurrentUser.Programs.Reqfields.YourDetailsPhone;
            //ShippingCb.Required = (App.Current.RootVisual as MainPage).CurrentUser.Programs.Reqfields.ShippingDetailsTitle;
            //ShippingFirstNameTb.Required = (App.Current.RootVisual as MainPage).CurrentUser.Programs.Reqfields.ShippingDetailsFirstName;
            //ShippingSurnameTb.Required = (App.Current.RootVisual as MainPage).CurrentUser.Programs.Reqfields.ShippingDetailsLastName;
            //BranchCb.Required = (App.Current.RootVisual as MainPage).CurrentUser.Programs.Reqfields.ShippingDetailsBranchName;
            //CostCb.Required = (App.Current.RootVisual as MainPage).CurrentUser.Programs.Reqfields.ShippingDetailsCostCenter;
            //AddressTb.Required = (App.Current.RootVisual as MainPage).CurrentUser.Programs.Reqfields.ShippingDetailsAddress;
            //SuburbTb.Required = (App.Current.RootVisual as MainPage).CurrentUser.Programs.Reqfields.ShippingDetailsSuburb;
            //StateTb.Required = (App.Current.RootVisual as MainPage).CurrentUser.Programs.Reqfields.ShippingDetailsState;
            //PostCodeTb.Required = (App.Current.RootVisual as MainPage).CurrentUser.Programs.Reqfields.ShippingDetailsPostCode;
            //CountryTb.Required = (App.Current.RootVisual as MainPage).CurrentUser.Programs.Reqfields.ShippingDetailsCountry;

            
        }

        
        //}

        private void BackButton_Click(object sender, RoutedEventArgs e)
        {
            clientWelcomePage wp = new clientWelcomePage();

            wp.Products = (App.Current.RootVisual as MainPage).CurrentUser.Programs.Products;
            wp.ClientNameTb.Text = (App.Current.RootVisual as MainPage).CurrentUser.Programs.merc_name;

            (App.Current.RootVisual as MainPage).LoadContent(wp);
        }

        private void NextButton_Click(object sender, RoutedEventArgs e)
        {
            // only process if all required items are filled in. 

            bool ok = true;

            if (!FirstNameTb.Validate())
                ok = false;

            if (!SurnameTb.Validate())
                ok = false;

            if (!EmailTb.Validate())
                ok = false;

            if (!TelephoneTb.Validate())
                ok = false;

            if (!ShippingFirstNameTb.Validate())
                ok = false;

            if (!ShippingSurnameTb.Validate())
                ok = false;

            if (!PostCodeTb.Validate())
                ok = false;

            if (!SuburbTb.Validate())
                ok = false;

            if (!StateTb.Validate())
                ok = false;

            if (!ShippingCb.Validate())
                ok = false;

            if (!TitleComboBox.Validate())
                ok = false;

            if (!AddressTb.Validate())
                ok = false;

            if (!CostCb.Validate())
                ok = false;

            if (!BranchCb.Validate())
                ok = false;

            if (ok)
            {
                // goto next step

                (App.Current.RootVisual as MainPage).LoadContent(new clientOrderReview());
            }
            else
            {
                System.Windows.Browser.HtmlPage.Window.Alert("Please fill in the fields marked with a red outline");
            }

        }

        private void CostCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DbBranches selectedCenter = (sender as ComboBoxExtension).Items[(sender as ComboBoxExtension).SelectedIndex] as DbBranches;

            string tempStreetName = "";
            string tempStreetNumber = "";
            
            if (selectedCenter.streetname != null)
            {
                tempStreetName = selectedCenter.streetname;
            }

            if (selectedCenter.streetnumber != null)
            {
                tempStreetNumber = selectedCenter.streetnumber;
            }


            AddressTb.Text = tempStreetNumber + " " + tempStreetName;

            if (selectedCenter.suburb == null)
            {
                SuburbTb.Text = "";
            }
            else
            {
                SuburbTb.Text = selectedCenter.suburb;
            }

            if (selectedCenter.state == null)
            {
                StateTb.Text = "";
            }
            else
            {
                StateTb.Text = selectedCenter.state;
            }

            if (selectedCenter.postcode == null)
            {
                PostCodeTb.Text = "";
            }
            else
            {
                PostCodeTb.Text = selectedCenter.postcode;
            }
        }
    }
}
